<?php
namespace app\api\controller;

use app\admin\model\dts\point\task\Log;
use app\admin\model\point\Perform;
use app\common\controller\Api;
use think\Db;

class Task extends Api {

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    //每天凌晨2点创建巡检任务
    public function performtask() {
        $date = date('Y-m-d', time());

        //查询新建、结束任务
        $list = Db::name('dts_point_task')->alias('task')
        ->field('task.*,time.type,time.is_even,time.weekarr,time.startperiod,time.endperiod')
        ->join([
            ["dts_point_time time", "time.id = task.point_time_id and ('". $date ."'>= time.nottime and '". $date ."' <= time.endtime)"],
        ])
        ->select();

        //当前时间
        $currtime = time();
        $nottime = date('Y-m-d', time());
        //当天周几
        $dayofweek = date('w', $currtime); 
        //当前周
        $currweek = date('W', $currtime); 
        //当前天
        $currday = date('d', $currtime);

        $dataArr = [];

        foreach($list as $items) {

            $tempArr = [];
            $idArr = array_unique(explode(',', $items['point_grouping_ids']));
            foreach($idArr as $id) {
                if($id) {
                    $tempArr[] = ['point_grouping_id' => $id, 'point_task_id' => $items['id'], 'admin_id' => $items['admin_id'], 'nottime' => date('Y-m-d', time()), 'startperiod' => $items['startperiod'], 'endperiod' => $items['endperiod']];
                }
            }

            $type = $items['type'];
            switch($type) {
                case 'day':
                    $dataArr = array_merge($dataArr, $tempArr);
                    //发送公众消息
                    break;
                case 'week':
                    $itemArr = explode(',', $items['weekarr']);
                    if(in_array($dayofweek, $itemArr)) {
                        $dataArr = array_merge($dataArr, $tempArr);
                        //发送公众消息
                    }
                    break;        
                case 'toweek':
                    $itemArr = explode(',', $items['weekarr']);
                    if(in_array($dayofweek, $itemArr)) {
                        if(($currweek % 2) == 0 && $items['is_even'] == 0) {//偶数周
                            $dataArr = array_merge($dataArr, $tempArr);
                            //发送公众消息
                        }
                        if(($currweek % 2) != 0 && $items['is_even'] != 0) {//单数周
                            $dataArr = array_merge($dataArr, $tempArr);
                            //发送公众消息
                        }
                    }
                    break;
                case 'month':
                    $itemArr = explode(',', $items['months']);
                    if(in_array($currday, $itemArr)) {
                        $dataArr = array_merge($dataArr, $tempArr);
                    }
                    break;  
            }
        }

        if($dataArr) {
            $taskLogModel = new \app\admin\model\dts\point\task\Log;
            $taskLogModel->saveAll($dataArr);
        }

        $this->success('巡检任务创建成功');
    }


    //每小时复位巡检任务
    public function resetask() {
        $list = Db::name('dts_point_task_log')->whereRaw('status = '.Log::STATUS_0.' or status = '.Log::STATUS_1)->select();

        //当前时间
        $currtime = strtotime(date('Y-m-d H:i:s', time()));

        $dataArr = [];

        foreach($list as $items) {

            $status = $items['status'];
            $nottimeS = strtotime($items['nottime'].' '.$items['startperiod']);
            $nottimeE = strtotime($items['nottime'].' '.$items['endperiod']);


            switch($status) {
                case Log::STATUS_0://未开始
                    if($currtime <= $nottimeE && $currtime >= $nottimeS) {
                        $dataArr[] = ['id' => $items['id'], 'status' => Log::STATUS_1];
                    }
                    if($currtime > $nottimeE) {
                        $dataArr[] = ['id' => $items['id'], 'status' => Log::STATUS_2];
                    }
                    break;
                case Log::STATUS_1://进行中
                    if($currtime > $nottimeE) {
                        $dataArr[] = ['id' => $items['id'], 'status' => Log::STATUS_2];
                    }
                    break;     
            }
        }

        if($dataArr) {
            $taskLogModel = new \app\admin\model\dts\point\task\Log;
            $taskLogModel->saveAll($dataArr);
        }

        $this->success('巡检任务复位成功');
    }

}